package com.tianji.common.utils;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public  class DelayedTask<T> implements Delayed {
    private T data;
    private Long delayTime;

    public DelayedTask() {

    }

    public DelayedTask(T data, Integer time) {
        this.data = data;
        this.delayTime = System.currentTimeMillis() + time * 1000;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long l = this.delayTime - System.currentTimeMillis();
        if (l < 0) {
            return 0;
        }
        return l;
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.delayTime - ((DelayedTask) o).delayTime);
    }
}